#include "STC8H.H"
#include "intrins.h"
#include "../main/configuration.h"
#include "../PWM/PWM.h"


/*#########################################变量#############################################*/

extern setting settings;


//unsigned char _pwm_duty;                           //pwm 占空比
//unsigned char _pwm_pre_duty;                    //风扇转速百分比





/*########################################函数###############################################*/
void pwm_init()								    //占空比时间  i %
{
    P_SW2 = 0x80;
    PWMB_CCER1 = 0x00;                          //写CCMRx前必须先清零CCERx关闭通道
    PWMB_CCMR1 = 0x60;                          //设置CC5为PWM1输出模式
    PWMB_CCER1 = 0x01;                          //使能CC5通道
    PWMB_CCR5 = 0;                                                              //设置占空比
    PWMB_ARR = pwm_period;                      //设置周期时间,25khz 3840
    PWMB_ENO = pwm_out;                          //使能PWM5（P17）端口输出
    PWMB_BKR = 0x80;                            //使能主输出
    PWMB_CR1 = 0x01;                            //开始计时

}


void pwm_adj()                                  //调整pwm占空比
{
    unsigned char data _pwm_pre_duty;
    unsigned char data _pwm_duty;
                           //计算转速百分比并转化成对应pwm占空比,赋值转速百分比给_pwm_pre_duty，占空比给_pwm_duty

    if (settings.f0.l[_control_point_count-1].temp>=_temp)
    {
        if (_temp<=settings.f0.l[0].temp)
        {
            _pwm_pre_duty=settings.f0.l[0].duty;
        }else
        {
            char i;
            for (i=0;i<(_control_point_count-1);i++)
            {
                
                if(settings.f0.l[i].temp>=_temp && _temp<=settings.f0.l[i+1].temp)
                {
                    _pwm_pre_duty=(_temp-settings.f0.l[i].temp)*(settings.f0.l[i+1].duty-settings.f0.l[i].duty)/(settings.f0.l[i+1].temp-settings.f0.l[i].temp)+settings.f0.l[i].duty;
                }
                
            }
        }




    }else
    {
        _pwm_pre_duty=100;
    }    

    switch (settings.f2)
    {
    case 1:                                  //100-0
        _pwm_duty=100-_pwm_pre_duty;
        break;
    
    case 2:                                 //100-20
        _pwm_duty=100-(_pwm_pre_duty*8)/10;
        break;
    default:                                //0-100
        _pwm_duty=_pwm_pre_duty;
        break;
    }



                      
    PWMB_CCR5 = _pwm_duty*pwm_period/100;               //调整占空比
}

void sc_init(unsigned long int data i)                             //sc测速信号模拟(hz)  0-95000
{
    P_SW2 = 0x80;
    
    PWMA_CCER2 = 0x00;                          //写CCMRx前必须先清零CCERx关闭通道
    PWMA_CCMR3 = 0x60;                          //设置CC3为PWM1输出模式
    PWMA_CCER2 = 0x01;                          //使能CC3通道
    
    PWMA_CCR3 = 50*pwm_period/100;                                              //设置占空比50%
    PWMA_PSCR = 28800;                          //设置pwm预分频（fosc/（PSCR+1））
    PWMA_ARR = fosc/28801/i-1;                  //设置周期时间
    PWMA_ENO = sc_out;                          //使能PWM3P（P14）端口输出
    PWMA_BKR = 0x80;                            //使能主输出
    PWMA_CR1 = 0x01;                            //开始计时
}
